package routers

import (
	"server/global"
	"server/internal/service"
	"server/pkg/app"
	"server/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type Resource struct{}

func NewResource() *Resource {
	return &Resource{}
}

func (r *Resource) NewResource(c *gin.Context) {
	param := &struct {
		Name         string `form:"name" binding:"required"`
		ChainName    string `form:"chainName" binding:"required"`
		Username     string `form:"username" binding:"required"`
		ChainAccount string `form:"chainAccount" binding:"required"`
		Price        int    `form:"price" binding:"required"`
		Data         string `form:"data" binding:"required"`
	}{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errRsp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errRsp)
		return
	}

	svc := service.New(c.Request.Context())
	err := svc.NewResource(param.Name, param.ChainName,
		param.Username, param.ChainAccount, param.Data, param.Price)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorNewResourceFail)
		return
	}

	response.ToSuccessResponse(nil)
}

func (r *Resource) GetMyResources(c *gin.Context) {
	param := &struct {
		Username string `form:"username" binding:"required"`
	}{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errRsp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errRsp)
		return
	}

	svc := service.New(c.Request.Context())
	resources, err := svc.GetMyResources(param.Username)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetResourceFail)
		return
	}

	response.ToSuccessResponse(resources)
}

func (r *Resource) GetOnSaleResources(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	resources, err := svc.GetOnSaleResources()
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetResourceFail)
		return
	}

	response.ToSuccessResponse(resources)
}

func (r *Resource) DeleteResource(c *gin.Context) {
	param := &struct {
		ID int `form:"id" binding:"required"`
	}{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errRsp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errRsp)
		return
	}

	svc := service.New(c.Request.Context())
	err := svc.DeleteResource(param.ID)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorDeleteResourceFail)
		return
	}

	response.ToSuccessResponse(nil)
}

func (r *Resource) StartSale(c *gin.Context) {
	param := &struct {
		ID int `form:"id" binding:"required"`
	}{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errRsp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errRsp)
		return
	}

	svc := service.New(c.Request.Context())
	err := svc.StartSale(param.ID)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorStartSaleFail)
		return
	}

	response.ToSuccessResponse(nil)
}

func (r *Resource) EndSale(c *gin.Context) {
	param := &struct {
		ID int `form:"id" binding:"required"`
	}{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errRsp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errRsp)
		return
	}

	svc := service.New(c.Request.Context())
	err := svc.EndSale(param.ID)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorEndSaleFail)
		return
	}

	response.ToSuccessResponse(nil)
}
